#[allow(unused_imports)]
use rust_decimal::Decimal;
use rusty_copybook::{Field, Spec, DecimalSpec, StringSpec, DecimalFormat, Sign};
use rusty_copybook::{Error, Result};

const RECORD_LEN: usize = {{total_length}};

#[allow(dead_code)]
pub struct {{struct_name}} {
    bytes: [u8; RECORD_LEN],
    {% for field_name in field_names -%}
    {{ field_name }}: Field,
    {% endfor -%}
}

impl {{struct_name}} {
    pub fn new(input: &[u8]) -> Result<{{struct_name}}>{
        if input.len() > RECORD_LEN {
            return Err(Error::BufferTooShort {
                expected: RECORD_LEN,
                actual: input.len()
            })
        }
        let mut bytes: [u8; RECORD_LEN] = [0; RECORD_LEN];
        bytes[..input.len()].copy_from_slice(input);
        {% for init in inits %}
        {{ init }}
        {% endfor %}
        Ok({{struct_name}}{
            bytes,
            {% for field_name in field_names -%}
            {{ field_name }},
            {% endfor -%}
        })
    }
    pub fn as_bytes(&self) -> &[u8]{
        &self.bytes
    }
    pub fn length(&self) -> u32 {
        RECORD_LEN as u32
    }

    {% for getter in getters -%}
    {{ getter }}
    {% endfor %}    
}
 
